package com.woniuxy.util;



import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class MyRedisUtil {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private RedisTemplate<String,Object> redisObjectTemplate;


    public void set(String key,String value){
        stringRedisTemplate.opsForValue().set(key,value);
    }

    public void set(String key,String value,long seconds){
        stringRedisTemplate.opsForValue().set(key,value,seconds, TimeUnit.SECONDS);
    }
    public String get(String key){return stringRedisTemplate.opsForValue().get(key);}
    public Boolean exists(String key){
       return stringRedisTemplate.hasKey(key);
    }

    public void increment(String key){
        stringRedisTemplate.opsForValue().increment(key);
    }
    public void del(String key){
        stringRedisTemplate.delete(key);
    }
    public void zadd(String key,String value,double score){
        stringRedisTemplate.opsForZSet().add(key,value,score);
    }
    public Double zscore(String key,String value){
        return stringRedisTemplate.opsForZSet().score(key,value);
    }
    public void zincrementBy(String key,String value,double score){
        stringRedisTemplate.opsForZSet().incrementScore(key,value,score);
    }
    public void zrem(String key,String value){
        stringRedisTemplate.opsForZSet().remove(key,value);
    }

    public Set<String> zrevrange(String key,Long start,Long end){
        return stringRedisTemplate.opsForZSet().reverseRange(key,start,end);
    }
    public Long zcard(String key){
        return stringRedisTemplate.opsForZSet().zCard(key);
    }
    public void hmset(String key, Map<Object,Object> map){
        stringRedisTemplate.opsForHash().putAll(key,map);
    }
    public void hincrBy(String key,Object hashKey,double value){
        stringRedisTemplate.opsForHash().increment(key,hashKey,value);
    }
    public Map<Object,Object> hgetall (String key){
        return stringRedisTemplate.opsForHash().entries(key);
    }
    public void  setObject(String key,Object value){
        redisObjectTemplate.opsForValue().set(key,value);
    }
    public void  setObject(String key,Object value,long seconds){
        redisObjectTemplate.opsForValue().set(key,value,seconds,TimeUnit.SECONDS);
    }
    public Object  getObject(String key){
       return redisObjectTemplate.opsForValue().get(key);
    }
}
